<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import NonConformanceModal from './modules/NonConformanceModal'
import ApprovalButtons from '@comp/approval/ApprovalButtons.vue'
import { postAction } from '@api/manage'

export default {
  name: 'MaterialNonConformanceList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    ApprovalButtons,
    NonConformanceModal
  },
  props: {
    status: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      description: '不合格处理单管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          fixed: 'left',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '单据类型',
          align: 'center',
          dataIndex: 'documentType_dictText',
          width: 80,
          fixed: 'left'
        },
        {
          title: '单据号',
          align: 'center',
          dataIndex: 'documentNumber',
          width: 80,
          fixed: 'left'
        },
        {
          title: '不合格单号',
          align: 'center',
          dataIndex: 'nonConformanceNumber',
          width: 80,
          fixed: 'left'
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'materialCode'
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName'
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'specificationModel'
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText'
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'quantity'
        },
        {
          title: '批次号',
          align: 'center',
          dataIndex: 'batchNumber'
        },
        {
          title: '物料批号',
          align: 'center',
          dataIndex: 'materialBatchNumber'
        },
        {
          title: '储位',
          align: 'center',
          dataIndex: 'storageLocation_dictText'
        },
        {
          title: '入库日期',
          align: 'center',
          dataIndex: 'entryDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '生产日期',
          align: 'center',
          dataIndex: 'productionDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '保质期（天）',
          align: 'center',
          dataIndex: 'shelfLife'
        },
        {
          title: '限用日期',
          align: 'center',
          dataIndex: 'expirationDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '处理结果',
          align: 'center',
          dataIndex: 'processingOpinion_dictText'
        },
        {
          title: '创建人',
          align: 'center',
          dataIndex: 'createBy_dictText'
        },
        {
          title: '创建部门',
          align: 'center',
          dataIndex: 'sysOrgCode_dictText'
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/qms/non-conformance/material/list',
        delete: '/qms/non-conformance/delete',
        deleteBatch: '/qms/non-conformance/deleteBatch',
      },
      dictOptions: {},
      serviceName: 'nonConformanceService',
      settingColumns: [
      ],
      disableMixinCreated: true
    }
  },
  created() {
    this.queryParam.status = this.status
    this.loadData()
  },
  computed: {
    filterColumns() {
      if (this.status !== 0) {
        return this.columns
      }
      return this.columns.filter(item => {
        return item.dataIndex !== 'action'
      })
    }
  },
  methods: {
    initDictConfig() {
    },
    handleAdd() {
      if (!this.selectionRows || this.selectionRows.length === 0) {
        this.$message.warning('请先选中一条记录')
        return
      }
      this.$refs.modalForm.edit(this.selectionRows[0])
      this.$refs.modalForm.title = '添加'
      this.$refs.modalForm.disableSubmit = false
      this.$refs.modalForm.nonConformanceType = 'material'
    },
    handleDetail(record) {
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = '详情'
      this.$refs.modalForm.disableSubmit = true
      this.$refs.modalForm.nonConformanceType = 'material'
    },
    handleCancel(record) {
      postAction('/qms/non-conformance/revoke', { id: record.id })
        .then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
          } else {
            this.$message.warning(res.message)
          }
        })
    }
  }
}
</script>

<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-if="status===0" @click="handleAdd" type="primary" icon="plus">添加</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <span style="float:right;">
          <a-popover title="自定义列" trigger="click" placement="leftBottom">
            <template slot="content">
              <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                <a-row style="width: 400px">
                  <template v-for="(item,index) in defColumns">
                    <template v-if="item.key!=='rowIndex'&& item.dataIndex!=='action'">
                        <a-col :span="12"><a-checkbox :value="item.dataIndex"><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox></a-col>
                    </template>
                  </template>
                </a-row>
              </a-checkbox-group>
            </template>
            <a><a-icon type="setting" />设置</a >
          </a-popover>
        </span>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="filterColumns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap1"
        @change="handleTableChange">
        <span slot="action" slot-scope="text, record" class="action">
          <approval-buttons
            :status="status"
            @success="loadData"
            :data-id="record.id"
            :service-name="serviceName"
            :variables="record"
          />
          <a @click="handleEdit(record)" v-if="status === 1">编辑</a>
          <a-popconfirm
            v-if="status === 3"
            title="确定撤销吗?"
            @confirm="() => handleCancel(record)">
            <a>撤销</a>
          </a-popconfirm>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item v-if="![2,3].includes(status)">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <non-conformance-modal ref="modalForm" @ok="modalFormOk"></non-conformance-modal>
  </a-card>
</template>

<style scoped>
@import '~@assets/less/common.less';

span.action {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
}

.action a {
  position: relative;
}

.action a:not(:first-child)::before {
  content: "";
  position: absolute;
  left: 0;
  width: 1px;
  height: 0.9em;
  background-color: #e8e8e8;
  margin: 0 -8px;
}
</style>